package com.keehin;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.swing.JOptionPane;

public class Database {

	private static Calendar cal;

	private static String thisYear;

	static {
		cal = Calendar.getInstance(Locale.getDefault());
		thisYear = String.valueOf(cal.get(Calendar.YEAR));
	}

	public static String DATE_FORMAT = "d-MM-yyyy";

	public static String DERBY_DATE_FORMAT = "yyyy-MM-d";

	public static SimpleDateFormat dateFormat = new SimpleDateFormat(
			DATE_FORMAT, Locale.getDefault());

	public static SimpleDateFormat derbyDateFormat = new SimpleDateFormat(
			DERBY_DATE_FORMAT, Locale.ENGLISH);

	private Connection conn;

	private Statement stmt;

	private Map<String, PreparedStatement> sqlMap;

	private String connStr;

	public void createSQL(String name, String sql) {
		if (sqlMap.get(name) == null) {
			try {
				PreparedStatement pstmt = conn.prepareStatement(sql);
				sqlMap.put(name, pstmt);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public PreparedStatement getStatement(String name) {
		return sqlMap.get(name);
	}

	public Database(Driver drvMan, String url, String user, String passwd)
			throws SQLException {
		DriverManager.registerDriver(drvMan);
		conn = DriverManager.getConnection(url, user, passwd);
		stmt = conn.createStatement();
		sqlMap = new HashMap<String, PreparedStatement>();

	}

	public Database(Driver drvMan) throws SQLException {
		this(drvMan, Database.getConnectString(), null, null);
	}

	public Connection getConnection() {
		return conn;
	}

	public ResultSet executeQuery(String query) {
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public int execSqlMap(String name, Object[] params) {
		PreparedStatement stmt = sqlMap.get(name);
		int row = 0;
		if (stmt != null)
			try {
				if (params != null) {
					for (int i = 0; i < params.length; i++) {
						stmt.setObject(i + 1, params[i]);
					}
				}
				row = stmt.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return row;
	}

	public ResultSet querySqlMap(String name, Object[] params) {
		PreparedStatement stmt = sqlMap.get(name);
		ResultSet rs = null;
		if (stmt != null)
			try {
				if (params != null) {
					for (int i = 0; i < params.length; i++) {
						stmt.setObject(i + 1, params[i]);
					}
				}
				rs = stmt.executeQuery();
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null, e.getMessage());
			}
		return rs;
	}

	public int executeUpdate(String sql) {
		int rowEffected = 0;
		try {
			rowEffected = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rowEffected;
	}

	public String createDerbyDateString(String s) {
		String x = null;
		try {
			x = derbyDateFormat.format(dateFormat.parse(s));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return x;
	}

	public String createDerbyDateString(java.util.Date d) {
		return derbyDateFormat.format(d);
	}

	public Date createDate(String s) {
		Date d = null;
		try {
			d = createDate(dateFormat.parse(s));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public Date createDate(java.util.Date date) {
		return new Date(date.getTime());
	}

	public static Date createDate(int year, int month, int day) {
		java.sql.Date date = null;
		try {
			cal.set(year, month - 1, day);
			date = new java.sql.Date(cal.getTimeInMillis());
		} catch (Exception e) {
		}
		return date;
	}

	public static String completeDate(String dateTxt) {
		int len = dateTxt.length();
		String s = dateTxt;
		if (len >= 3 && len < 8) {
			if (dateTxt.charAt(len - 1) == '-')
				s = dateTxt + thisYear;
			else
				s = dateTxt + "-" + thisYear;
		}
		return s;
	}

	public static String getConnectString() {
		String s = null;
		try {
			BufferedReader bf = new BufferedReader(new FileReader(
					"database.conf"));
			s = bf.readLine();
			bf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return s;
	}

}
